package link;

import java.util.Stack;

public class ReverseWords {

    public static void main(String[] args) {
        String a = "a good   example";
        String s = reverseWords(a);
        System.out.println(s);

    }

    public static String reverseWords(String s) {
        if( s == null || s.length() == 0){return s;}
        StringBuilder sb = new StringBuilder();
        String[] splits = s.split(" ");

        Stack<String> aa = new Stack<>();
        for(int i = splits.length - 1; i > -1;i--){
            if(splits[i].length() > 0) {
                sb.append(splits[i].trim()).append(" ");
            }
        }
        return sb.toString().trim();


    }

}
